SlideShare une entreprise Scribd logo
Client-side Scripting Languages Introduction to Javascript
Plan du course Javascript – Pourquoi?, Ou?, Qu-est ce que c’est? Comment ajouter Javascript au HTML La syntaxe du Javascript  Document Object Model Exemples
Javascript – Pourquoi?, Ou?, Qu-est ce que c’est? Au début – simple texte + images + liens Des pages “live” ont été nécessaires sur le web  Netscape a invente “LiveScript” en 1995 LiveScript a change le nom plus tard en Javascript Javascript – fonctionne sur des navigateurs, permet de l'accès dynamique à la page html  Le langage a été interprété dans un façon différent de navigateurs différents
Include Javascript into HTML Script inclus dans la page html  <script type=”text/javascript”> //code script </script> Script inclus dans un fichier extérieur <script src=”fisier_surse.js”></script>
Quand est le code exécuté Si le tag “script” est inclus dans le tag “head” Le script est exécuté lorsque la page est chargée - avant de rendre  Si le script est inclus dans le corps page  Le script est exécuté lorsque le balisage est
Syntaxe du Javascript Similaire avec C & Java Les variables n’ont pas du type Les variables sont déclaré en utilisant le mot “var”  var x=5, y=7; Les fonctions sont déclaré en utilisant le mot “function” function sum(x,y) { var rez=x+y; return rez;} Les fonctions sont appelées comme en C/Java var suma=sum(2,5);
Les objets du Javascript Les objets ont des methodes (fonctions) Proprietes Exemple var student={nom: &quot;ion&quot;, an:2, notes:{mate:9, pc:8}}; alert(student.nom +&quot;<br>&quot; ); alert(student.an +&quot;<br>&quot;);
References pour les objets du Javascript Math http://www.w3schools.com/jsref/jsref_obj_math.asp   String http://www.w3schools.com/jsref/jsref_obj_string.asp   Array http://www.w3schools.com/jsref/jsref_obj_array.asp   Date http://www.w3schools.com/jsref/jsref_obj_date.asp
Examples <script type=&quot;text/javascript&quot;> function printValue() //declare a function { var x=Math.random()*10; //compute the value of x as a random value between 0 and 10 alert(x); //display an alert containing the value of x var y=&quot;a random text&quot;; //create a variable of type string alert(y.length); //display an alert containing the length of y //!!! length is a property and not a method alert(y.substr(0,5)); //compute and display as an alert the substring of y between the first and the 5 th  character alert(x+&quot; &quot;+y.length+&quot; &quot;+y.substr(5,y.length)); //display as an alert the string formed by x, length of y and the substring formed from the 5 th  character of y until the last one } </script>
Javascript exemple - analyse “ +” est utilise pour aider les strings  “ alert” est utilise pour afficher d’information pendant le développement. Il ne doit pas etre utilise en applications Les objets peuvent avoir des méthodes comme y.substr(0,5) et propriétés comme y.length
Evénements Les éléments du HTML peuvent détecter quand l’utilisateur a des interactions avec eux Exemples d’interactions Mouse over (mouse out) Click Key pressed Blur change Nous pouvons écrire du code Javascript pour traiter les interactions
Events examples <script type=“text/javascript”> function youClicked(element) {element.innerHTML=&quot;you clicked this element&quot;;} function youMousedOver() {alert(&quot;mouse over detected&quot;); } </script> <h1 onclick=&quot;alert('youclicked');youClicked( this );&quot; onmouseover=&quot;youMousedOver()&quot;> Introduction dans la programmation web</h1>
Evénements Qu’est que c’est “this” Html element User interacts event1 event2 Event1 a une fonction javascript La fonction javascript doit savoir quel élément a modifier “ this” est la référence a l’objet qui doit être modifie
DOM DOM=Document Object Model DOM = une representation des elements de la page HTML
DOM The DOM tree contains nodes which can be Html elements Text  The tree elements can be accessed By traversing the tree (See Data structures course) By accessing them directly by name (getElementsByTagName) By accessing them directly by id (getElementById) Addressing them directly (as in an array) The root of the DOM tree is the document
Methods and properties document.write(“text”) Adds the “text” to the given page If the page is finished loading it rewrites it Example <script type=&quot;text/javascript&quot;> function printValue() { var x=Math.random()*10; alert(x); var y=&quot;a random text&quot;; alert(y.length); alert(y.substr(0,5)); alert(x+&quot; &quot;+y.length+&quot;!!!&quot;+y.substr(5,y.length)); document.write(x+&quot; &quot;+y.length+&quot;!!!&quot;+y.substr(5,y.length)); } </script>
DOM Methods and properties getElementsByTagName Returns an array of elements with a given name The we need to know the position of the element we need to modify inside the array function modifySecondH1() { var headersArray=document.getElementsByTagName(&quot;h1&quot;); //retrieves all the elements whose names are h1 //elements’ numbering in the array starts at 0 headersArray[1].innerHTML=Math.random()*5; }
DOM Methods and properties document.getElementById The most used method to access an element of a html page We have to be careful to set ids for the elements function modifyFirstH1() { //retrieve the element with the id h1id1 var h1Element=document.getElementById(&quot;h1id1&quot;); //set the HTML value for the document h1Element.innerHTML=&quot;new title&quot;; }
DOM objects methods and properties Direct access to the element Predefined collections Forms Links  Images document.forms[0].username.value – accesses the first form in the document and sets the value property for the username input
Example – using javascript to validate forms When a form is submitted we need to validate it first on the client-side The form should be validated before submitting The event should be added to the submit button For example we want to check if 2 passwords have the same value and if the username is longer than 4 characters
Validate forms with Javascript – example (I) function validateForm(){ var usernameElement=document.getElementById(&quot;username&quot;); var passwordElement=document.getElementById(&quot;password&quot;); var rePasswordElement=document.getElementById(&quot;repassword&quot;); if(passwordElement.value!=rePasswordElement.value || passwordElement.value.length==0) {alert(&quot;please make sure the password is entered the same twice&quot;);return;} if (usernameElement.value.length<4) {alert(&quot;please make sure the username is longer than 4 letters&quot;);return; } document.forms[0].submit(); }
Example of form validation with Javascript (II) <form action=&quot;script.php&quot; method=&quot;POST&quot;> nom d'utilisateur<input type=&quot;text&quot; id=&quot;username&quot; /><br/> mot de passe<input type=&quot;password&quot; id=&quot;password&quot; /> <br/> mot de passe encore une fois <input id=&quot;repassword&quot; type=&quot;password&quot;> <br/> <input type=&quot;button&quot; value=&quot;send&quot; onclick=&quot;validateForm();&quot;/> </form>
Javascript debugging Firebug – extension for Firefox Allows debugging of scripts Step by step execution Adding breakpoints Watch expressions Visualize the DOM tree
Javascript debugging example

Contenu connexe

PDF
Cours javascript
PDF
Javascript
PDF
PDF
Jquery - introduction au langage
PDF
Crs javascript
PDF
Jquery
PPTX
Initiation au JavaScript
PPT
Présentation jQuery pour débutant
Cours javascript
Javascript
Jquery - introduction au langage
Crs javascript
Jquery
Initiation au JavaScript
Présentation jQuery pour débutant

Tendances (13)

PPTX
Introduction à AngularJS
PDF
Manualjquery
PPTX
Introduction à ajax
PPTX
Introduction à React JS
PDF
Introduction a jQuery
PDF
Cours JavaScript
PDF
Javascript
PDF
Jquery : les bases
PDF
CocoaHeads Rennes #10 : Mock Objects
PPTX
Requêtes HTTP synchrones et asynchrones
PDF
Partie 2: Angular
PPTX
Introduction à React
Introduction à AngularJS
Manualjquery
Introduction à ajax
Introduction à React JS
Introduction a jQuery
Cours JavaScript
Javascript
Jquery : les bases
CocoaHeads Rennes #10 : Mock Objects
Requêtes HTTP synchrones et asynchrones
Partie 2: Angular
Introduction à React
Publicité

En vedette (20)

PPT
Usability and accessibility on the web
PPTX
Programarea calculatoarelor c2
PPTX
IPW HTML course
PPTX
Ce mă fac când o să fiu mare - optiuni pentru o cariera in IT
PPTX
IPW 2eme course - HTML
PPT
C5 Javascript French
PPTX
Linked Open Data in Romania
PPT
IPW 3rd Course - CSS
PPT
C5 Javascript
PPT
Introduction dans la Programmation Web Course 1
PPT
utilisabilite et accessibilite au web
PPT
HTML 5 - intro - en francais
PPT
IPW Course 3 CSS
PPT
Intro to HTML5
PPT
Introduction to Web Programming - first course
PPTX
Css+html
PPT
Présentation html5
PDF
Cours HTML/CSS
PDF
Beautiful CSS : Structurer, documenter, maintenir
PDF
Application web php5 html5 css3 bootstrap
Usability and accessibility on the web
Programarea calculatoarelor c2
IPW HTML course
Ce mă fac când o să fiu mare - optiuni pentru o cariera in IT
IPW 2eme course - HTML
C5 Javascript French
Linked Open Data in Romania
IPW 3rd Course - CSS
C5 Javascript
Introduction dans la Programmation Web Course 1
utilisabilite et accessibilite au web
HTML 5 - intro - en francais
IPW Course 3 CSS
Intro to HTML5
Introduction to Web Programming - first course
Css+html
Présentation html5
Cours HTML/CSS
Beautiful CSS : Structurer, documenter, maintenir
Application web php5 html5 css3 bootstrap
Publicité

Similaire à C5 Javascript (20)

PDF
Johnny-Five : Robotique et IoT en JavaScript
PPT
Cours JavaScript.ppt
PPTX
Formation java script
PPTX
javascript cours developpement nbhdjcbhdcjbn
PPTX
Introduction-au-JavaScript + programmation orientée objet.pptx
PDF
Javascript mémo.pdf
PPT
Introduction à JavaScript
PDF
732587539-cours-tp-JavaScript-3Sfffffffffffffffffffffffffffffffffffffffffffff...
PPT
introJavaScript.ppt
PPTX
Cours javascript v1
PDF
le Langage de programmation JavaScript.pdf
DOCX
les fonctions, les procedures, les tableaux en Javascript Pc.docx
PPT
initiation au javascript
PPT
Eléments de base du langage JavaScript (1).ppt
PPTX
JavaScript prise en main et fondamentaux
PDF
cours developpement web javascript 2023/2024
PPTX
Les bases du javascript
PDF
Initiation au JavaScript
PPT
cours baser sur les bases de JAVASCRIPT.ppt
PDF
Javascript pour les Développeurs WEB
Johnny-Five : Robotique et IoT en JavaScript
Cours JavaScript.ppt
Formation java script
javascript cours developpement nbhdjcbhdcjbn
Introduction-au-JavaScript + programmation orientée objet.pptx
Javascript mémo.pdf
Introduction à JavaScript
732587539-cours-tp-JavaScript-3Sfffffffffffffffffffffffffffffffffffffffffffff...
introJavaScript.ppt
Cours javascript v1
le Langage de programmation JavaScript.pdf
les fonctions, les procedures, les tableaux en Javascript Pc.docx
initiation au javascript
Eléments de base du langage JavaScript (1).ppt
JavaScript prise en main et fondamentaux
cours developpement web javascript 2023/2024
Les bases du javascript
Initiation au JavaScript
cours baser sur les bases de JAVASCRIPT.ppt
Javascript pour les Développeurs WEB

Plus de Vlad Posea (14)

PPTX
Design thinking
PPTX
Talentul meu – mersul pe bicicletă
PPTX
Programarea calculatoarelor - Limbajul C
PPTX
Social semantic web
PDF
Ghidul Bobocului de la Facultatea de Automatica si Calculatoare vers 2011-2012
PDF
Json tutorial
PDF
Javascript ajax tutorial
PPT
Studiu Referitor La Insertia Pe Piata Muncii (1)
PPT
Aplicații Web Semantice - Descriere Proiect
PPT
Stagii In Strainatate
PPT
Student si/sau Angajat
PDF
Ghidul bobocului de la Facultatea de Automatica si Calculatoare
PPT
Tips & Tricks Proiect
PPT
Boboc Advisory Board Intalnire 1
Design thinking
Talentul meu – mersul pe bicicletă
Programarea calculatoarelor - Limbajul C
Social semantic web
Ghidul Bobocului de la Facultatea de Automatica si Calculatoare vers 2011-2012
Json tutorial
Javascript ajax tutorial
Studiu Referitor La Insertia Pe Piata Muncii (1)
Aplicații Web Semantice - Descriere Proiect
Stagii In Strainatate
Student si/sau Angajat
Ghidul bobocului de la Facultatea de Automatica si Calculatoare
Tips & Tricks Proiect
Boboc Advisory Board Intalnire 1

Dernier (6)

PDF
presentation_with_intro_compressee IEEE EPS France
PDF
Modems expliqués- votre passerelle vers Internet.pdf
PDF
FORMATION EN Programmation En Langage C.pdf
PPTX
Presentation_Securite_Reseaux_Bac+2.pptx
PDF
FORMATION COMPLETE EN EXCEL DONE BY MR. NYONGA BRICE.pdf
PDF
Tendances tech 2025 - SFEIR & WENVISION.pdf
presentation_with_intro_compressee IEEE EPS France
Modems expliqués- votre passerelle vers Internet.pdf
FORMATION EN Programmation En Langage C.pdf
Presentation_Securite_Reseaux_Bac+2.pptx
FORMATION COMPLETE EN EXCEL DONE BY MR. NYONGA BRICE.pdf
Tendances tech 2025 - SFEIR & WENVISION.pdf

C5 Javascript

  • 1. Client-side Scripting Languages Introduction to Javascript
  • 2. Plan du course Javascript – Pourquoi?, Ou?, Qu-est ce que c’est? Comment ajouter Javascript au HTML La syntaxe du Javascript Document Object Model Exemples
  • 3. Javascript – Pourquoi?, Ou?, Qu-est ce que c’est? Au début – simple texte + images + liens Des pages “live” ont été nécessaires sur le web Netscape a invente “LiveScript” en 1995 LiveScript a change le nom plus tard en Javascript Javascript – fonctionne sur des navigateurs, permet de l'accès dynamique à la page html Le langage a été interprété dans un façon différent de navigateurs différents
  • 4. Include Javascript into HTML Script inclus dans la page html <script type=”text/javascript”> //code script </script> Script inclus dans un fichier extérieur <script src=”fisier_surse.js”></script>
  • 5. Quand est le code exécuté Si le tag “script” est inclus dans le tag “head” Le script est exécuté lorsque la page est chargée - avant de rendre Si le script est inclus dans le corps page  Le script est exécuté lorsque le balisage est
  • 6. Syntaxe du Javascript Similaire avec C & Java Les variables n’ont pas du type Les variables sont déclaré en utilisant le mot “var” var x=5, y=7; Les fonctions sont déclaré en utilisant le mot “function” function sum(x,y) { var rez=x+y; return rez;} Les fonctions sont appelées comme en C/Java var suma=sum(2,5);
  • 7. Les objets du Javascript Les objets ont des methodes (fonctions) Proprietes Exemple var student={nom: &quot;ion&quot;, an:2, notes:{mate:9, pc:8}}; alert(student.nom +&quot;<br>&quot; ); alert(student.an +&quot;<br>&quot;);
  • 8. References pour les objets du Javascript Math http://www.w3schools.com/jsref/jsref_obj_math.asp String http://www.w3schools.com/jsref/jsref_obj_string.asp Array http://www.w3schools.com/jsref/jsref_obj_array.asp Date http://www.w3schools.com/jsref/jsref_obj_date.asp
  • 9. Examples <script type=&quot;text/javascript&quot;> function printValue() //declare a function { var x=Math.random()*10; //compute the value of x as a random value between 0 and 10 alert(x); //display an alert containing the value of x var y=&quot;a random text&quot;; //create a variable of type string alert(y.length); //display an alert containing the length of y //!!! length is a property and not a method alert(y.substr(0,5)); //compute and display as an alert the substring of y between the first and the 5 th character alert(x+&quot; &quot;+y.length+&quot; &quot;+y.substr(5,y.length)); //display as an alert the string formed by x, length of y and the substring formed from the 5 th character of y until the last one } </script>
  • 10. Javascript exemple - analyse “ +” est utilise pour aider les strings “ alert” est utilise pour afficher d’information pendant le développement. Il ne doit pas etre utilise en applications Les objets peuvent avoir des méthodes comme y.substr(0,5) et propriétés comme y.length
  • 11. Evénements Les éléments du HTML peuvent détecter quand l’utilisateur a des interactions avec eux Exemples d’interactions Mouse over (mouse out) Click Key pressed Blur change Nous pouvons écrire du code Javascript pour traiter les interactions
  • 12. Events examples <script type=“text/javascript”> function youClicked(element) {element.innerHTML=&quot;you clicked this element&quot;;} function youMousedOver() {alert(&quot;mouse over detected&quot;); } </script> <h1 onclick=&quot;alert('youclicked');youClicked( this );&quot; onmouseover=&quot;youMousedOver()&quot;> Introduction dans la programmation web</h1>
  • 13. Evénements Qu’est que c’est “this” Html element User interacts event1 event2 Event1 a une fonction javascript La fonction javascript doit savoir quel élément a modifier “ this” est la référence a l’objet qui doit être modifie
  • 14. DOM DOM=Document Object Model DOM = une representation des elements de la page HTML
  • 15. DOM The DOM tree contains nodes which can be Html elements Text The tree elements can be accessed By traversing the tree (See Data structures course) By accessing them directly by name (getElementsByTagName) By accessing them directly by id (getElementById) Addressing them directly (as in an array) The root of the DOM tree is the document
  • 16. Methods and properties document.write(“text”) Adds the “text” to the given page If the page is finished loading it rewrites it Example <script type=&quot;text/javascript&quot;> function printValue() { var x=Math.random()*10; alert(x); var y=&quot;a random text&quot;; alert(y.length); alert(y.substr(0,5)); alert(x+&quot; &quot;+y.length+&quot;!!!&quot;+y.substr(5,y.length)); document.write(x+&quot; &quot;+y.length+&quot;!!!&quot;+y.substr(5,y.length)); } </script>
  • 17. DOM Methods and properties getElementsByTagName Returns an array of elements with a given name The we need to know the position of the element we need to modify inside the array function modifySecondH1() { var headersArray=document.getElementsByTagName(&quot;h1&quot;); //retrieves all the elements whose names are h1 //elements’ numbering in the array starts at 0 headersArray[1].innerHTML=Math.random()*5; }
  • 18. DOM Methods and properties document.getElementById The most used method to access an element of a html page We have to be careful to set ids for the elements function modifyFirstH1() { //retrieve the element with the id h1id1 var h1Element=document.getElementById(&quot;h1id1&quot;); //set the HTML value for the document h1Element.innerHTML=&quot;new title&quot;; }
  • 19. DOM objects methods and properties Direct access to the element Predefined collections Forms Links Images document.forms[0].username.value – accesses the first form in the document and sets the value property for the username input
  • 20. Example – using javascript to validate forms When a form is submitted we need to validate it first on the client-side The form should be validated before submitting The event should be added to the submit button For example we want to check if 2 passwords have the same value and if the username is longer than 4 characters
  • 21. Validate forms with Javascript – example (I) function validateForm(){ var usernameElement=document.getElementById(&quot;username&quot;); var passwordElement=document.getElementById(&quot;password&quot;); var rePasswordElement=document.getElementById(&quot;repassword&quot;); if(passwordElement.value!=rePasswordElement.value || passwordElement.value.length==0) {alert(&quot;please make sure the password is entered the same twice&quot;);return;} if (usernameElement.value.length<4) {alert(&quot;please make sure the username is longer than 4 letters&quot;);return; } document.forms[0].submit(); }
  • 22. Example of form validation with Javascript (II) <form action=&quot;script.php&quot; method=&quot;POST&quot;> nom d'utilisateur<input type=&quot;text&quot; id=&quot;username&quot; /><br/> mot de passe<input type=&quot;password&quot; id=&quot;password&quot; /> <br/> mot de passe encore une fois <input id=&quot;repassword&quot; type=&quot;password&quot;> <br/> <input type=&quot;button&quot; value=&quot;send&quot; onclick=&quot;validateForm();&quot;/> </form>
  • 23. Javascript debugging Firebug – extension for Firefox Allows debugging of scripts Step by step execution Adding breakpoints Watch expressions Visualize the DOM tree